
package com.netease.xone.fragment;

import protocol.XoneCallBack;
import protocol.XoneService;
import protocol.meta.CheckVersionInfo;
import Utils.XoneUtil;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.netease.xone.R;
import com.netease.xone.activity.ActivityUpdateApp;

public class FragAbout extends FragmentBase implements OnClickListener {

    private TextView mTxVersion, mTxNewVersion;
    private int mTransactionId;
    private boolean mBHasNewVersion = false;
    private CheckVersionInfo mCheckVersionInfo;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        getActivity().setTitle(R.string.setting_item_about);
        View view = inflater.inflate(R.layout.fragment_about, container, false);
        findViews(view);
        return view;
    }

    private void findViews(View view) {
        mTxVersion = (TextView)view.findViewById(R.id.appVersionNum);
        mTxNewVersion = (TextView)view.findViewById(R.id.newVersionTip);

        mTxVersion.setText("V" + XoneUtil.getNumberVersion(getActivity()));

        LinearLayout checkVersion = (LinearLayout)view.findViewById(R.id.about_layout_checkversion);
        checkVersion.setOnClickListener(this);

        TextView tv = (TextView)view.findViewById(R.id.about_function);
        tv.setOnClickListener(this);
        tv = (TextView)view.findViewById(R.id.about_manual);
        tv.setOnClickListener(this);
        tv = (TextView)view.findViewById(R.id.about_clause);
        tv.setOnClickListener(this);
        mTransactionId = XoneService.getInstance().doCheckAppVersion();

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        XoneService.getInstance().addListener(mXoneCallBack);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        XoneService.getInstance().removeListener(mXoneCallBack);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.about_layout_checkversion:
                if (mBHasNewVersion) {
                    if (mCheckVersionInfo != null) {
                        ActivityUpdateApp.lunch(getActivity(), mCheckVersionInfo.updateAddress,
                                mCheckVersionInfo.description, mCheckVersionInfo.version);
                    }
                } else {
                    showToast(R.string.update_newest);
                }
                break;
            case R.id.about_function:
                showToast(R.string.about_function);
                break;
            case R.id.about_manual:
                showToast(R.string.about_manual);
                break;
            case R.id.about_clause:
                showToast(R.string.about_clause);
                break;
        }

    }
    private void switchToNewVersionMode(boolean hasNew){
        if(hasNew){
            mTxNewVersion.setVisibility(View.VISIBLE);
            mBHasNewVersion = true;
        }else {
            mTxNewVersion.setVisibility(View.GONE);
            mBHasNewVersion = false;
        }
    }

    private XoneCallBack mXoneCallBack = new XoneCallBack() {

        @Override
        public void onGetVersionInfo(int transactionId, CheckVersionInfo versionInfo) {
            if (versionInfo != null) {
                mCheckVersionInfo = versionInfo;
                switchToNewVersionMode(true);
            } else {
                switchToNewVersionMode(false);
            }
        };

        @Override
        public void onGetVersionInfoError(int transactionId, int errCode, String err) {
            if (mTransactionId == transactionId) {
                showToast(err);
            }
        };

    };
}
